package developers;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Locale;

import co.yeye.test.Constance;
import co.yeye.test.MainActivity;
import co.yeye.test.R;

/**
 * Created by yeye on 15/3/1.
 */
public class BeDeveloper extends ActionBarActivity {

    ImageLoader imageLoader;
    DisplayImageOptions options;

    ImageView img;
    EditText experience,language,ide,purpose;
    Button tittle,btn_confirm;

    String img_file_path="-1",img_file_name="-1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.be_developer);

        ActionBar actionBar = this.getSupportActionBar();
        actionBar.setTitle("成为开发者");
        actionBar.setBackgroundDrawable(getResources().getDrawable(R.color.blue));

        imageLoader = MainActivity.getInstance().getImageLoader();
        options = MainActivity.getInstance().getOptionCache();

        img = (ImageView) findViewById(R.id.img);
        experience = (EditText) findViewById(R.id.experience);
        language = (EditText) findViewById(R.id.language);
        ide = (EditText) findViewById(R.id.ide);
        purpose = (EditText) findViewById(R.id.target);
        tittle = (Button) findViewById(R.id.tittle);
        btn_confirm = (Button) findViewById(R.id.btn_confirm);

        tittle.setVisibility(View.GONE);

        setListener();

    }

    private void setListener(){
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 请拍摄真实的照片
                Toast.makeText(getBaseContext(),"无图无真相，请拍摄您真人哦~",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent,100);
            }
        });

        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 检查是否为空
                if(experience.getText().length()<=0 || language.getText().length()<=0 ||
                        ide.getText().length()<=0 || purpose.getText().length()<=0){
                    Constance.setTittle("请填写必要信息",getResources().getColor(R.color.red),3000,tittle);
                    return;
                }
                // 检查是否拍摄了照片
                if(img_file_path.equals("-1")){
                    Constance.setTittle("请拍摄照片",getResources().getColor(R.color.red),3000,tittle);
                    return;
                }
                // 上传
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==100){
            if(resultCode==RESULT_OK){
                String sdStatus = Environment.getExternalStorageState();
                if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
                    Log.i("TestFile",
                            "SD card is not avaiable/writeable right now.");
                    return;
                }
                String name = new DateFormat().format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg";
                Toast.makeText(this, name, Toast.LENGTH_LONG).show();
                Bundle bundle = data.getExtras();
                Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据，并转换为Bitmap图片格式

                FileOutputStream b = null;
                File file = new File("/sdcard/myImage/");
                file.mkdirs();// 创建文件夹
                String fileName = "/sdcard/myImage/"+name;
                img_file_path = fileName;
                img_file_name = name;

                try {
                    b = new FileOutputStream(fileName);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        b.flush();
                        b.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                imageLoader.displayImage("file://"+fileName,img,options,null);
            }
        }
    }
}
